<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <h1 class="login-title">💕 执手记</h1>
        <p class="login-subtitle">情侣专属记账与情感互动平台</p>
      </div>

      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" class="login-form">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            size="large"
            prefix-icon="User"
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            size="large"
            prefix-icon="Lock"
            show-password
            @keyup.enter="handleLogin"
          />
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            size="large"
            class="login-button"
            :loading="loading"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-footer">
        <router-link to="/register" class="link">还没有账号？立即注册</router-link>
        <router-link to="/forgot-password" class="link">忘记密码？</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '@/api'
import { useUserStore } from '@/stores/user'

const router = useRouter()
const userStore = useUserStore()

const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = reactive({
  username: '',
  password: ''
})

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能小于6位', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return

  await loginFormRef.value.validate(async (valid) => {
    if (valid) {
      loading.value = true
      try {
        const res = await login(loginForm)
        
        // 保存 token 和用户信息
        userStore.setToken(res.data.token)
        userStore.setUserInfo(res.data.userInfo)
        
        ElMessage.success('登录成功')
        router.push('/home')
      } catch (error) {
        console.error('登录失败：', error)
      } finally {
        loading.value = false
      }
    }
  })
}
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #ffeef8 0%, #fff5f7 100%);
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 420px;
  background: #fff;
  border-radius: 24px;
  padding: 40px;
  box-shadow: 0 8px 24px rgba(255, 154, 158, 0.3);
}

.login-header {
  text-align: center;
  margin-bottom: 40px;
}

.login-title {
  font-size: 36px;
  font-weight: bold;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  margin-bottom: 10px;
}

.login-subtitle {
  font-size: 14px;
  color: #999;
}

.login-form {
  margin-top: 30px;
}

.login-button {
  width: 100%;
  background: linear-gradient(90deg, #ff9a9e 0%, #fecfef 100%);
  border: none;
  font-size: 16px;
  font-weight: bold;
}

.login-footer {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
}

.link {
  color: #ff9a9e;
  text-decoration: none;
  font-size: 14px;
  transition: all 0.3s;
}

.link:hover {
  color: #ff8a8e;
  text-decoration: underline;
}

/* 移动端适配 */
@media (max-width: 768px) {
  .login-container {
    padding: 16px;
    align-items: flex-start;
    padding-top: 60px;
  }

  .login-card {
    padding: 32px 24px;
    border-radius: 20px;
    max-width: 100%;
  }

  .login-header {
    margin-bottom: 32px;
  }

  .login-title {
    font-size: 30px;
  }

  .login-subtitle {
    font-size: 13px;
  }

  .login-form {
    margin-top: 24px;
  }

  .login-footer {
    flex-direction: column;
    gap: 12px;
    text-align: center;
  }

  .link {
    font-size: 13px;
  }

  /* 优化输入框触摸体验 */
  :deep(.el-input__inner) {
    font-size: 16px; /* 防止 iOS 自动缩放 */
    height: 48px;
  }

  :deep(.el-button--large) {
    height: 48px;
    font-size: 16px;
  }
}

/* 小屏手机适配 */
@media (max-width: 576px) {
  .login-container {
    padding: 12px;
    padding-top: 40px;
  }

  .login-card {
    padding: 28px 20px;
    border-radius: 16px;
  }

  .login-title {
    font-size: 26px;
  }

  .login-subtitle {
    font-size: 12px;
  }
}

/* 横屏适配 */
@media (orientation: landscape) and (max-height: 500px) {
  .login-container {
    padding-top: 20px;
  }

  .login-card {
    padding: 20px;
  }

  .login-header {
    margin-bottom: 20px;
  }

  .login-title {
    font-size: 24px;
  }

  .login-form {
    margin-top: 16px;
  }
}
</style>

